package cn.edu.leetcode.其他;

public class 位1的个数 {
    public static void main(String[] args) {

        System.out.println(hammingWeight(2));
    }

    // you need to treat n as an unsigned value
    public static int hammingWeight(int n) {

        int num = 0;

        char[] chars = Integer.toBinaryString(n).toCharArray();
        for (char c: chars) {
            if (c == '1') num++;
        }
        return num;
    }
}
